package icu.d4peng.basic.knife4j.extension;

import java.util.HashMap;
import java.util.Map;

import icu.d4peng.basic.knife4j.properties.Knife4jProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;

import com.github.xiaoymin.knife4j.core.conf.GlobalConstants;

import io.swagger.v3.oas.models.OpenAPI;

/**
 * @author d4peng d4peng@qq.com
 * @date 2023-11-25 19:42:44
 */
public class Knife4jOpenApiCustomizer implements GlobalOpenApiCustomizer {

	private static final Logger LOGGER = LoggerFactory.getLogger(Knife4jOpenApiCustomizer.class);

	private final Knife4jProperties knife4jProperties;

	public Knife4jOpenApiCustomizer(Knife4jProperties knife4jProperties) {
		this.knife4jProperties = knife4jProperties;
	}

	@Override
	public void customise(OpenAPI openApi) {
		LOGGER.debug("Knife4j OpenApiCustomizer");
		if (knife4jProperties.isEnabled()) {
			Knife4jProperties.Knife4jSetting setting = knife4jProperties.getSetting();
			OpenApiExtensionResolver openApiExtensionResolver = new OpenApiExtensionResolver(setting,
					knife4jProperties.getDocuments());
			// 解析初始化
			openApiExtensionResolver.start();
			Map<String, Object> objectMap = new HashMap<>();
			objectMap.put(GlobalConstants.EXTENSION_OPEN_SETTING_NAME, setting);
			objectMap.put(GlobalConstants.EXTENSION_OPEN_MARKDOWN_NAME, openApiExtensionResolver.getMarkdownFiles());
			openApi.addExtension(GlobalConstants.EXTENSION_OPEN_API_NAME, objectMap);
		}
	}

}
